﻿function initSystemData() {
    var _InitDataObject = [
        {
            ID: "",
            Name: "常用网站",
            URLs: [
                { ID: "", URL: "Edit.html", Text: "链接管理" },
                { ID: "", URL: "http://www.baidu.com", Text: "百度" },
                { ID: "", URL: "http://www.sina.com.cn", Text: "新浪" },
                { ID: "", URL: "http://www.qq.com", Text: "腾讯" },
                { ID: "", URL: "http://www.163.com", Text: "网易" },
                { ID: "", URL: "http://www.youku.com", Text: "优酷" },
                { ID: "", URL: "http://www.ifeng.com", Text: "凤凰" },
                { ID: "", URL: "http://weibo.com/", Text: "新浪微博" },
                { ID: "", URL: "http://qzone.qq.com/", Text: "QQ空间" },
                { ID: "", URL: "https://www.google.com/", Text: "谷歌" }
            ]
        }, {
            ID: "",
            Name: "IFCA",
            URLs: [
                { ID: "", URL: "http://10.122.10.221/esale/", Text: "电子开盘(原型)" },
                { ID: "", URL: "http://10.122.10.221/msale/", Text: "移动应用(原型)" }
            ]
        }, {
            ID: "",
            Name: "Google",
            URLs: [
                { ID: "", URL: "https://www.google.com", Text: "Google" },
                { ID: "", URL: "https://plus.google.com/", Text: "Google+" },
                { ID: "", URL: "https://mail.google.com", Text: "Gmail" },
                { ID: "", URL: "https://www.google.com/contacts/", Text: "Contacts" },
                { ID: "", URL: "https://calendar.google.com", Text: "Calendar" },
                { ID: "", URL: "https://translate.google.com/", Text: "Translate" },
                { ID: "", URL: "https://code.google.com", Text: "Code" },
                { ID: "", URL: "https://appengine.google.com/", Text: "Appengine" }
            ]
        }, {
            ID: "",
            Name: "Microsoft",
            URLs: [
                { ID: "", URL: "http://www.microsoft.com/zh-cn/default.aspx", Text: "Microsoft" },
                { ID: "", URL: "http://msdn.microsoft.com/", Text: "MSDN" },
                { ID: "", URL: "https://skydrive.live.com/", Text: "SkyDrive" },
                { ID: "", URL: "http://www.codeplex.com", Text: "CodePlex" },
                { ID: "", URL: "http://www.microsoft.com/zh-cn/download/default.aspx", Text: "Download Center" },
                { ID: "", URL: "http://cn.bing.com/", Text: "Bing" }
            ]
        }
    ];
    //数据结构
    localStorage.clear();
    var TypeIDArray = []; //类型ID数据
    for (var i = 0, j = _InitDataObject.length; i < j; i++) {
        var TypeObj = _InitDataObject[i];
        var id = fun_Common_NewGUID();
        TypeObj.ID = id;
        TypeIDArray.push(id);
        var TypeNameKeyString = "Type_Name_" + id;
        //保存类型名称:前加"Type_Name_"
        localStorage.setItem(TypeNameKeyString, TypeObj.Name);

        var UrlIDArray = [];
        for (var m = 0, k = TypeObj.URLs.length; m < k; m++) {
            var URLObj = TypeObj.URLs[m];
            var urlID = fun_Common_NewGUID();
            URLObj.ID = urlID;
            var URLTextKeyString = "URL_Text_" + urlID;
            //保存URL的文本:key值前加"URL_Text_"
            localStorage.setItem(URLTextKeyString, URLObj.Text);

            var URLURLKeyString = "URL_URL_" + urlID;
            //保存URL的地址:key值前加"URL_URL_"
            localStorage.setItem(URLURLKeyString, URLObj.URL);

            var URLParentIDKeyString = "URL_ParentID_" + urlID;
            //保存每个URL的父节点ID
            localStorage.setItem(URLParentIDKeyString, id);

            UrlIDArray.push(urlID);
        }
        var TypeURLIDKeyString = "Type_URLIDS_" + id;
        //保存当前类型下面的地址所有ID字符串,前加"Type_URLS_"
        localStorage.setItem(TypeURLIDKeyString, UrlIDArray.join(","));
    }
    //所有类型ID,以,号分开的字符串,
    localStorage.setItem("Type_AllID_String", TypeIDArray.join(","));

    //初始化的时候.不设置这个值,这样就可以做服务器同步
    //localStorage.setItem("localStorage_LastModifyDate", new Date().getTime());
}

function CreateDocumentElement() {
    var TempDoc = document.createDocumentFragment();
    var Type_AllID_Array = localStorage.getItem("Type_AllID_String").split(",");
    for (var i = 0, j = Type_AllID_Array.length; i < j; i++) {
        var Type_Name_Key = "Type_Name_" + Type_AllID_Array[i];
        var Type_Name_Value = localStorage.getItem(Type_Name_Key);
        //fieldset
        var fieldsetObj = document.createElement("fieldset");
        var legendObj = document.createElement("legend");
        legendObj.innerHTML = Type_Name_Value;
        fieldsetObj.appendChild(legendObj);

        TempDoc.appendChild(fieldsetObj);

        var Type_URLIDS_Key = "Type_URLIDS_" + Type_AllID_Array[i];
        var Type_URLIDS_Value = localStorage.getItem(Type_URLIDS_Key);
        if (Type_URLIDS_Value == null) {
            continue;
        }
        var Type_URLIDS_Array = Type_URLIDS_Value.split(",");
        //ul
        var ulObj = document.createElement("ul");
        for (var k = 0, l = Type_URLIDS_Array.length; k < l; k++) {
            var URL_Text_Key = "URL_Text_" + Type_URLIDS_Array[k];
            var URL_Text_Value = localStorage.getItem(URL_Text_Key);

            var URL_URL_Key = "URL_URL_" + Type_URLIDS_Array[k];
            var URL_URL_Value = localStorage.getItem(URL_URL_Key);

            var liObj = document.createElement("li");
            var aObj = document.createElement("a");
            //aObj.title = URL_Text_Value;
            aObj.title = URL_URL_Value;
            aObj.onclick = URLonClickEvent;
            //aObj.setAttribute("onclick", "URLonClickEvent()");
            //aObj.attachEvent("onclick", URLonClickEvent);
            //aObj.href = URL_URL_Value;
            aObj.href = "javascript:;";
            aObj.id = Type_URLIDS_Array[k];
            aObj.innerHTML = URL_Text_Value;

            liObj.appendChild(aObj);
            ulObj.appendChild(liObj);
        }

        fieldsetObj.appendChild(ulObj);
        //TempDoc.appendChild(fieldsetObj);
    }
    return TempDoc;
}
//点击链接的时候跳转
function URLonClickEvent() {
    var a_id = event.srcElement.id;
    var URL_URL_Key = "URL_URL_" + a_id;
    var URL_URL_Value = localStorage.getItem(URL_URL_Key);
    if (URL_URL_Value != null) {
        location.href = URL_URL_Value;
    }
}
